%% setup 
use_top_prod = 2;

profit_cutoff_scope = 35/12;

R_fc = 100;% number of simulations for brewery entry

%% brewer level entry data construction
N_endo_dev = length(endo_dev_ind);
brewer_dev_name = cell(N_endo_dev, 1);
for k = 1 : N_endo_dev
    brewer_dev_name(k) = prod_attributes{endo_dev_ind(k)}.brewer;
end
[brewer_dev_list, ~, brewer_dev_id] = unique(brewer_dev_name);
N_brewer_dev = length(brewer_dev_list);
tmp = tabulate(brewer_dev_id);
max_N_brewer_prod = max(tmp(:,2));% max number of products per brewer
clear tmp

% initialize
%(1) brewer entry
brewer_entry = zeros(N_mkt, N_brewer_dev);

%(2)in every market-brewery combination, rank products by dev_profit_one
max_profit_top = nan(N_mkt, N_brewer_dev, max_N_brewer_prod);
min_profit_top = nan(N_mkt, N_brewer_dev, max_N_brewer_prod);
max_profit_top_boot = nan(N_mkt, N_brewer_dev, max_N_brewer_prod);
min_profit_top_boot = nan(N_mkt, N_brewer_dev, max_N_brewer_prod);

%(3) fixed cost covariates (must be brewer specific)
brewer_fc_x = nan(N_mkt, N_brewer_dev, N_fc_x);

%(4)IV
brewer_fc_iv0 = nan(N_mkt, N_brewer_dev, N_fc_iv);

for k = 1 : N_brewer_dev
    brewer_entry(:, k) = sum(entry_sim(:, brewer_dev_id==k), 2)>0;
    
    % from large to small, based on the profit lower bounds
    [~, ind_k0] = sort(-dev_profit_all(:, brewer_dev_id==k), 2);    
    brewer_k_ind = find(brewer_dev_id==k);    
    ind_k = brewer_k_ind(ind_k0); %N_mkt x (# of products by this brewer)    
    N_top_prod_k = sum(brewer_dev_id==k);
    
    for m = 1 : N_mkt 
        max_profit_top(m, k, 1:N_top_prod_k) = dev_profit_one(m, ind_k(m, 1:N_top_prod_k));        
        min_profit_top(m, k, 1:N_top_prod_k) = dev_profit_all(m, ind_k(m, 1:N_top_prod_k));        
        brewer_fc_iv0(m, k, :) = fc_iv0(m, ind_k(m, 1), :);
        brewer_fc_x(m, k, :) = fc_x(m, ind_k(m, 1), :);        
    end

    for nb = 1 : N_boot
        [~, ind_k0] = sort(-dev_profit_one_boot0(:, brewer_dev_id==k, nb), 2);
        ind_k = brewer_k_ind(ind_k0);
        N_top_prod_k = sum(brewer_dev_id==k);

        for m = 1 : N_mkt
            max_profit_top_boot(m, k, 1:N_top_prod_k, nb)...
                = dev_profit_one_boot0(m, ind_k(m, 1:N_top_prod_k), nb);
            min_profit_top_boot(m, k, 1:N_top_prod_k, nb)...
                = dev_profit_all_boot0(m, ind_k(m, 1:N_top_prod_k), nb);
        end
    end    
end    

brewer_entry_tr = reshape(brewer_entry', [N_mkt*N_brewer_dev 1]);
max_profit_top_vec = reshape(permute(max_profit_top, [2 1 3]), [N_mkt*N_brewer_dev max_N_brewer_prod]);
min_profit_top_vec = reshape(permute(min_profit_top, [2 1 3]), [N_mkt*N_brewer_dev max_N_brewer_prod]);
max_profit_top_vec_boot = reshape(permute(max_profit_top_boot, [2 1 3 4]), [N_mkt*N_brewer_dev max_N_brewer_prod N_boot]);
min_profit_top_vec_boot = reshape(permute(min_profit_top_boot, [2 1 3 4]), [N_mkt*N_brewer_dev max_N_brewer_prod N_boot]);
brewer_fc_iv_tr = reshape(permute(brewer_fc_iv0, [2 1 3]), [N_mkt*N_brewer_dev N_fc_iv]);
brewer_fc_x_tr = reshape(permute(brewer_fc_x, [2 1 3]), [N_mkt*N_brewer_dev N_fc_x]);

% brewer level IVs
brewer_g_ar_tr = ones(size(brewer_fc_iv_tr, 1), 1);
for nk = 1 : use_top_prod
    tmp = gen_iv(min_profit_top_vec(:, nk), ...
        max_profit_top_vec(:, nk), profit_cutoff_scope, brewer_fc_iv_tr);
    brewer_g_ar_tr = [brewer_g_ar_tr, tmp];
end
clear tmp


brewer_g_ar_tr_boot = zeros(N_mkt*N_brewer_dev, size(brewer_g_ar_tr, 2), N_boot);

for nb = 1 : N_boot
    
    brewer_g_ar_tr_boot_nb = ones(size(brewer_fc_iv_tr, 1), 1);
    for nk = 1 : use_top_prod
        tmp = gen_iv(min_profit_top_vec_boot(:, nk, nb), ...
            max_profit_top_vec_boot(:, nk, nb), profit_cutoff_scope, brewer_fc_iv_tr);
        brewer_g_ar_tr_boot_nb = [brewer_g_ar_tr_boot_nb, tmp];
    end
    brewer_g_ar_tr_boot(:, :, nb) = brewer_g_ar_tr_boot_nb;


end
 
% simulated fc cost shocks
fc_err = randn(N_mkt*N_brewer_dev, max_N_brewer_prod, 1, R_fc);

clear brewer_dev_name brewer_dev_list brewer_dev_id brewer_entry max_profit_top min_profit_top...
    max_profit_top_boot min_profit_top_boot brewer_fc_x brewer_g_ar brewer_fc_iv0 brewer_fc_iv_tr...
    brewer_k_ind ind_k ind_k0 N_top_prod_k brewer_g_ar_tr_boot_nb tmp
